package com.showcai.controller.certificate;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.showcai.controller.base.BaseController;
import com.showcai.def.Constant;
import com.showcai.module.*;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.CertificateBatchService;
import com.showcai.service.CertificateClassifyService;
import com.showcai.service.CertificateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2017-7-3.
 */
@RequestMapping("/certificate")
@Controller
public class CertificateContorller extends BaseController {
    @Autowired
    CertificateService certificateService;
    @Autowired
    CertificateBatchService certificateBatchService;
    @Autowired
    CertificateClassifyService certificateClassifyService;

    /**
     * 卡劵管理
     *
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/certificateList")
    public ModelAndView getCertificateList(Integer page, Integer pageSize) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/certificate/certificateList");
        Map<String, String> map = this.showRequestParameters();
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page",String.valueOf(page));
        map.put("pageSize",String.valueOf(pageSize));
        PageInfo pageInfo = certificateService.getPageInfoCertificateList(map);
        mov.addObject("pageinfo", pageInfo);
        mov.addObject("bean", map);
        return mov;
    }


    /**
     * 跳转到卡劵修改页面
     *
     * @param certificateId
     * @return
     */
    @RequestMapping(value = "/certificate")
    public ModelAndView certificate(String certificateId) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/certificate/certificate");

       Certificate certificate= certificateService.getCertificateByCertificateId(certificateId);
       mov.addObject("certificate",certificate);
        return mov;
    }

    /**
     * 修改卡券方法
     *
     * @param request     certificateBatch
     * @param request
     * @param certificate
     * @return
     */
    @RequestMapping(value = "/modifyCertificate",   method = RequestMethod.POST)
    @ResponseBody
    public String modifyCertificate(Certificate certificate) {
        ResultObj resultObj=new ResultObj();
        try {
             certificateService.modifyCertificate(certificate);

        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return resultObj.toJson();


    }



    /**
     * 查询卡劵使用记录
     *
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/certificateUseLogList", produces = "application/json;charset=UTF-8")
    public ModelAndView certificateUseLogList(HttpServletRequest request, Integer page, Integer pageSize) {
        ModelAndView mov = new ModelAndView("/certificate/certificateUseLogList.jsp");
        Map<String, String> map = this.showRequestParameters();
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        PageHelper.startPage(page, pageSize);
        List<CertificateUserdLog> certificateUserdLogList = certificateService.getCertificateUserdLogList(map);
        PageInfo<CertificateUserdLog> pageinfo = new PageInfo<>(certificateUserdLogList);
        mov.addObject("pageinfo", pageinfo);
        return mov;
    }

}
